package com.fwtai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

@SpringBootApplication
public class Frontend implements EnvironmentAware{

  public static void main(final String[] args){
    SpringApplication.run(Frontend.class,args);
  }

  @Override
  public void setEnvironment(final Environment environment){
    final Integer port = environment.getProperty("server.port",Integer.class);
    try {
      final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
      while (interfaces.hasMoreElements()){
        final NetworkInterface networkInterface = interfaces.nextElement();
        final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
        while (addresses.hasMoreElements()){
          final InetAddress address = addresses.nextElement();
          if (!address.isLoopbackAddress() && address.isSiteLocalAddress()) {
            System.out.println("http://"+ address.getHostAddress()+":"+port+"/index.html");
          }
        }
      }
    } catch (final Exception ignored){
      System.out.println("http://127.0.0.1:"+port+"/index.html");
    }
  }
}